package com.bos.controller;

import com.bos.controller.base.BaseController;
import com.bos.controller.base.imp.BaseControllerImp;
import com.bos.domain.Region;
import com.bos.domain.Staff;
import com.bos.service.IRegionService;
import com.bos.utils.BOSUtil;
import com.bos.utils.PageBean;
import com.bos.utils.PinYin4jUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

/**
 * @author Administrator
 */
@Controller
public class RegionController extends BaseControllerImp<Region> implements BaseController<Region> {

    @Autowired
    private IRegionService regionService;

    private final String LIST="page/base/region";

    @Autowired
    public RegionController(PageBean pageBean) {
        super(pageBean);
    }

    /**
     * 接收文件*/
    @RequestMapping(value = "/importRegionFile",method = RequestMethod.POST)
    @ResponseBody
    public String importRegionFile(MultipartFile regionfile, HttpServletResponse res) throws IOException {
        String realPath = BOSUtil.getSession().getServletContext().getRealPath("/uploads/regionfile");
        File file=new File(realPath);
        if (!file.exists()){
            file.mkdir();
        }
        /*获得上传的文件名字*/
        String filename = regionfile.getOriginalFilename();
        filename= UUID.randomUUID().toString().replace("-", " ")+"_"+filename;
        /*把文件存到指定路径下*/
        try {
            regionfile.transferTo(new File(realPath ,filename));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件存储失败");
        }
        //处理上传的Excel文件
        HSSFWorkbook hssfWorkbook=new
                HSSFWorkbook(new FileInputStream(new File(realPath,filename)));
        HSSFSheet sheet1 = hssfWorkbook.getSheet("sheet1");
        ArrayList<Region> regionlist=new ArrayList<>();
        for (Row rows:sheet1){
            if (rows.getRowNum()==0) {
                continue;
            }
            String id = rows.getCell(0).getStringCellValue();
            String province = rows.getCell(1).getStringCellValue();
            String city = rows.getCell(2).getStringCellValue();
            String district = rows.getCell(3).getStringCellValue();
            String postcode = rows.getCell(4).getStringCellValue();
            Region region=new Region(id,province,city,district,postcode,null,null,null);

            //构造简码
            province = province.substring(0, province.length() - 1);
            city = city.substring(0, city.length() - 1);
            district = district.substring(0, district.length() - 1);
            String info = province + city + district;
            String[] headByString = PinYin4jUtils.getHeadByString(info);
            String shortcode = StringUtils.join(headByString);
            //城市编码---->>shijiazhuang
            String citycode = PinYin4jUtils.hanziToPinyin(city, "");

            region.setShortcode(shortcode);
            region.setCitycode(citycode);
            regionlist.add(region);
        }

        regionService.save(regionlist);
        //删除临时文件
        new File(realPath,filename).delete();
        return "ok";
    }

    @RequestMapping(value = "/RegionPageQuery",method = RequestMethod.GET)
    @ResponseBody
    public String pageQuery(int page ,int rows){
        BOSUtil.getSession().setAttribute("regionPage",page);
        BOSUtil.getSession().setAttribute("regionRows",rows);
        setPageAndRows(page,rows);
        regionService.pageQuery(super.pageBean);
        return java2json(new String[]{"currentPage", "pageSize", "detachedCriteria","subareas"});
    }


}
